package org.jeecg.modules.param.sysparam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.sysparam.entity.BikeSysParam;
import org.jeecg.modules.param.sysparam.mapper.BikeSysParamMapper;
import org.jeecg.modules.param.sysparam.service.IBikeSysParamService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Description: bike_sys_param
 * @Author: 月月鸟
 * @Date:   2021-05-18
 * @Version: V1.0
 */
@Service
public class BikeSysParamServiceImpl extends ServiceImpl<BikeSysParamMapper, BikeSysParam> implements IBikeSysParamService {
    @Resource
    private BikeSysParamMapper bikeSysParamMapper;
    @Override
    public void updateByIds(List<BikeSysParam> bikeSysParam) {
        for (BikeSysParam tb:bikeSysParam){
            BikeSysParam sysParam = new BikeSysParam();
            sysParam.setId(tb.getId());
            sysParam.setPValue(tb.getPValue());
            bikeSysParamMapper.updateById(sysParam);
        }
    }

    @Override
    public Result<?> editOrAdd(List<BikeSysParam> bikeSysParams) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String err = null ;
        //循环校验
        for (int i = 0 ; i < bikeSysParams.size() ; i++){
            BikeSysParam bikeSysParam = bikeSysParams.get(i);
            //1到100
            if (bikeSysParam.getPUnit().equals("1")){
                String regex = "^(?:0|[1-9][0-9]?|100)$";
                boolean matches = Pattern.matches(regex, bikeSysParam.getPValue());
                if (!matches){
                    if (err!=null){
                        err=err+","+bikeSysParam.getMemo();
                    }else {
                        err=bikeSysParam.getMemo();
                    }
                }
            }
            if (bikeSysParam.getPUnit().equals("3")){
                String regex = "^\\d*\\.{0,1}\\d{0,1}$";
                boolean matches = Pattern.matches(regex, bikeSysParam.getPValue());
                if (!matches){
                    if (err!=null){
                        err=err+","+bikeSysParam.getMemo();
                    }else {
                        err=bikeSysParam.getMemo();
                    }
                }
            }
            // 温度 1-100
            if (bikeSysParam.getPUnit().equals("2")){
                String regex = "^(?:0|[1-9][0-9]?|100)$";
                boolean matches = Pattern.matches(regex, bikeSysParam.getPValue());
                if (!matches){
                    if (err!=null){
                        err=err+","+bikeSysParam.getMemo();
                    }else {
                        err=bikeSysParam.getMemo();
                    }
                }
            }
            //分钟 正整数
            if (bikeSysParam.getPUnit().equals("4")){
                String regex = "^[1-9]\\d*$";
                boolean matches = Pattern.matches(regex, bikeSysParam.getPValue());
                if (!matches){
                    if (err!=null){
                        err=err+","+bikeSysParam.getMemo();
                    }else {
                        err=bikeSysParam.getMemo();
                    }
                }
            }
        }
        if (StringUtils.isEmpty(err)){
            for (int i = 0 ; i < bikeSysParams.size() ; i++){
                BikeSysParam bikeSysParam = bikeSysParams.get(i);
                //如果是系统用户则新增
                if (bikeSysParam.getUserId().equals(BikeConstant.param_sys_user)){
                    bikeSysParam.setUserId(sysUser.getUsername());
                    bikeSysParam.setId(null);
                    bikeSysParam.setCreateBy(null);
                    bikeSysParam.setCreateTime(new Date());
                    bikeSysParam.setUpdateBy(null);
                    bikeSysParam.setUpdateTime(null);
                    bikeSysParamMapper.insert(bikeSysParam);
                }else {
                    bikeSysParamMapper.updateById(bikeSysParam);
                }
            }
            return Result.OK("编辑成功");
        }else {
            return Result.error("格式错误："+err);
        }

    }

    @Override
    public List<BikeSysParam> getList() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //警报参数
        String[] split = BikeConstant.warning_param.split(",");
        QueryWrapper<BikeSysParam> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",sysUser.getUsername());
        List<BikeSysParam> bikeSysParams = bikeSysParamMapper.selectList(wrapper);

        if (bikeSysParams.size()==0){
            QueryWrapper<BikeSysParam> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("user_id",BikeConstant.param_sys_user);
            wrapper1.in("p_name",split);
            List<BikeSysParam> bikeSysParams1 = bikeSysParamMapper.selectList(wrapper1);
            for (int i = 0 ; i<bikeSysParams1.size();i++){
                BikeSysParam bikeSysParam = bikeSysParams1.get(i);
                if (StringUtils.isEmpty(bikeSysParam.getPName())){
                    bikeSysParams1.remove(i);
                }
            }
            return bikeSysParams1;
        }else {

            return bikeSysParams;
        }
    }

    @Override
    public Result<?> getParameterByName(String name) {
        QueryWrapper<BikeSysParam> wrapper = new QueryWrapper<>();
        wrapper.eq("p_name",name);
        BikeSysParam bikeSysParam = bikeSysParamMapper.selectOne(wrapper);
        if (bikeSysParam!=null){
            return Result.ok1(1,bikeSysParam.getPValue());
        }else {
            return Result.error("未找到参数");
        }
    }
}
